package tool;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import mlProject.DocModel;
import mlProject.DocModel.Feature;

public class ComputeIDF {
	public static Map<String,Double> idf=new HashMap<String,Double>();
	public static void getIdf(){
		String path = "src/main/resources/"+"newTrain.txt";
		FileReader fr;
		try {
			fr = new FileReader(path);
			BufferedReader br = new BufferedReader(fr);
			String row;
			while ((row = br.readLine()) != null) {
				String[] words = row.split(" ");
				//double max=-1.0;
				for (int i = 1; i < words.length - 1; i++) {
					String[] kv = words[i].split(":");
					String key=kv[0];
					if(!idf.containsKey(key))
						idf.put(key, 1.0);
					else
						idf.put(key, idf.get(key)+1);
				}
			}
			br.close();
			fr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for(Map.Entry<String, Double> it:idf.entrySet()){
			Double temp=it.getValue();
			temp=Math.log(3200.0/temp)/Math.log(2);
			idf.put(it.getKey(),temp);
		}
	}
}
